package com.emellend.api.r;

import com.emellend.api.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;
/**
 * RadioGroup的简单示例
 * @author tiangh
 *
 */
public class RadioGroup1 extends Activity implements OnCheckedChangeListener,OnClickListener {

	private RadioGroup mRadioGroup;
	private TextView mChoice;
	private String selection="You have selected: ";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.radio_group_1);
		init();
	}
	private void init() {
		mRadioGroup = (RadioGroup) this.findViewById(R.id.menu);
		mChoice = (TextView) this.findViewById(R.id.choice);
		mChoice.setText(selection+mRadioGroup.getCheckedRadioButtonId());
		addRadioGroup();
		addListener();
	}
	
	/**
	 * 通过编码的方式往RadioGroup中添加按钮
	 */
	private void addRadioGroup() {
		
		RadioButton radioButton=new RadioButton(this);
		radioButton.setText("Snack");
		radioButton.setId(123);
		LinearLayout.LayoutParams params=
				new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		
		mRadioGroup.addView(radioButton, 0, params);
	}
	
	private void addListener() {
		mRadioGroup.setOnCheckedChangeListener(this);
		this.findViewById(R.id.clear).setOnClickListener(this);
	}
	
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		String none="(none)";
		mChoice.setText(selection+(checkedId==View.NO_ID?none:checkedId));
	}
	public void onClick(View v) {
		mRadioGroup.clearCheck();
	}

}
